package com.boluo.businessManagement.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.boluo.businessManagement.domain.BusMentGoods;
import com.boluo.businessManagement.dto.BusMentStoreDTO;
import com.boluo.businessManagement.mapper.BusMentGoodsMapper;
import com.boluo.common.core.domain.TreeSelect;
import com.boluo.common.core.domain.entity.BMGoodsCategory;
import com.boluo.common.core.domain.entity.SysDept;
import com.boluo.common.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boluo.businessManagement.mapper.BusMentGoodsCategoryMapper;
import com.boluo.businessManagement.domain.BusMentGoodsCategory;
import com.boluo.businessManagement.service.IBusMentGoodsCategoryService;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 物品分类信息Service业务层处理
 * 
 * @author boluo
 * @date 2023-05-02
 */
@Service
public class BusMentGoodsCategoryServiceImpl implements IBusMentGoodsCategoryService 
{
    @Resource
    private BusMentGoodsCategoryMapper busMentGoodsCategoryMapper;
    @Resource
    private BusMentGoodsMapper busMentGoodsMapper;

    /**
     * 查询物品分类信息
     * 
     * @param id 物品分类信息主键
     * @return 物品分类信息
     */
    @Override
    public BusMentGoodsCategory selectBusMentGoodsCategoryById(Long id)
    {
        return busMentGoodsCategoryMapper.selectBusMentGoodsCategoryById(id);
    }

    /**
     * 查询物品分类信息列表
     * 
     * @param busMentStoreDTO 物品分类信息
     * @return 物品分类信息
     */
    @Override
    public List<BusMentGoodsCategory> selectBusMentGoodsCategoryList(BusMentStoreDTO busMentStoreDTO)
    {
        return busMentGoodsCategoryMapper.selectBusMentGoodsCategoryList(busMentStoreDTO);
    }

    /**
     * 新增物品分类信息
     * 
     * @param busMentGoodsCategory 物品分类信息
     * @return 结果
     */
    @Override
    public int insertBusMentGoodsCategory(BusMentGoodsCategory busMentGoodsCategory)
    {
        // 校验参数
        Assert.isNull(busMentGoodsCategory.getId(),"你是?");
        Assert.hasLength(busMentGoodsCategory.getCategoryName(),"分类名不能为空");

        return busMentGoodsCategoryMapper.insertBusMentGoodsCategory(busMentGoodsCategory);
    }

    /**
     * 修改物品分类信息
     * 
     * @param busMentGoodsCategory 物品分类信息
     * @return 结果
     */
    @Override
    public int updateBusMentGoodsCategory(BusMentGoodsCategory busMentGoodsCategory)
    {
        Assert.notNull(busMentGoodsCategory.getId(),"非法参数");
        Assert.hasLength(busMentGoodsCategory.getCategoryName(),"分类名不能为空");

        return busMentGoodsCategoryMapper.updateBusMentGoodsCategory(busMentGoodsCategory);
    }

    /**
     * 批量删除物品分类信息
     * 
     * @param ids 需要删除的物品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentGoodsCategoryByIds(Long[] ids)
    {
        return busMentGoodsCategoryMapper.deleteBusMentGoodsCategoryByIds(ids);
    }

    /**
     * 删除物品分类信息信息
     * 
     * @param id 物品分类信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentGoodsCategoryById(Long id)
    {
        // 校验参数
        Assert.notNull(id,"你是什么牛马");
        BusMentGoodsCategory busMentGoodsCategory = busMentGoodsCategoryMapper.selectBusMentGoodsCategoryById(id);
        Assert.notNull(busMentGoodsCategory,"非法数据");
        //删除分类时连同子分类一起删除
        List<BusMentGoodsCategory> busMentGoodsCategories = busMentGoodsCategoryMapper.selectBusMentGoodsCategoryListByparentId(id);
        busMentGoodsCategories.add(busMentGoodsCategory);
        //删除分类时如当前分类连同子分类下的商品还有库存则不可以删除
        //
        List<BusMentGoods> busMentGoods = busMentGoodsMapper.selectByCategoryId(id);
        Assert.state(busMentGoods.size()>0,"该分类下有物品不能删除");


        List<Long> ids = busMentGoodsCategories.stream().map(BusMentGoodsCategory::getId).collect(Collectors.toList());
        Long[] array = ids.toArray(new Long[ids.size()]);
        return busMentGoodsCategoryMapper.deleteBusMentGoodsCategoryByIds(array);
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<BMGoodsCategory> categorys) {

        List<BMGoodsCategory> categoryTrees = buildDeptTree(categorys);
        return categoryTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<BMGoodsCategory> selectBMGoodsCategoryList() {

        return busMentGoodsCategoryMapper.selectBMGoodsCategoryList();
    }

    @Override
    public List<BusMentGoodsCategory> selectBusMentGoodsCategorylistNoPage() {
         return busMentGoodsCategoryMapper.selectBusMentGoodsCategorylistNoPage();
    }

    /**
     * 构建前端所需要树结构
     *
     * @param categorys 部门列表
     * @return 树结构列表
     */
    public List<BMGoodsCategory> buildDeptTree(List<BMGoodsCategory> categorys)
    {
        List<BMGoodsCategory> returnList = new ArrayList<BMGoodsCategory>();
        List<Long> tempList = new ArrayList<Long>();
        for (BMGoodsCategory category : categorys)
        {
            tempList.add(category.getId());
        }
        for (BMGoodsCategory category : categorys)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categorys, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categorys;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<BMGoodsCategory> categorys, BMGoodsCategory t)
    {
        // 得到子节点列表
        List<BMGoodsCategory> childList = getChildList(categorys, t);
        t.setChildren(childList);
        for (BMGoodsCategory tChild : childList)
        {
            if (hasChild(categorys, tChild))
            {
                recursionFn(categorys, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BMGoodsCategory> getChildList(List<BMGoodsCategory> list, BMGoodsCategory t)
    {
        List<BMGoodsCategory> tlist = new ArrayList<BMGoodsCategory>();
        Iterator<BMGoodsCategory> it = list.iterator();
        while (it.hasNext())
        {
            BMGoodsCategory n = (BMGoodsCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BMGoodsCategory> list, BMGoodsCategory t)
    {
        return getChildList(list, t).size() > 0;
    }
}
